package com.bh.ly.oa.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.bh.ly.oa.R;
import com.bh.ly.oa.adapter.GoodSaleListAdapter;
import com.bh.ly.oa.util.Config;
import com.bh.ly.oa.util.DateUtil;
import com.bh.ly.oa.util.RequestListType;

//TODO  NumberException
public class GoodsSaleDeptDtlActivity extends BaseListActiivity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.good_sale_list);
		setURLTYPE(Config.listGoodsSaleDept);
		
		initDatas();
	}

	protected void initDatas() {
		loadingView = findViewById(R.id.loading_view);
		loadingFaildView = (TextView) findViewById(R.id.loading_fiald_view);
		ImageButton button = (ImageButton) findViewById(R.id.back_button);
		ImageButton rearch_button = (ImageButton) findViewById(R.id.rearch_button);
		rearch_button.setVisibility(View.VISIBLE);
		rearch_button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(GoodsSaleDeptDtlActivity.this, SearchGoodsSaleDeptDtlActivity.class);
				startActivityForResult(intent, RequestListType.SEARCH_REQUEST_CODE_GoodSale);
			}
		});
		m_footerView = LayoutInflater.from(this).inflate(R.layout.loading, null);
		button.setVisibility(View.VISIBLE);
		button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
		
		listView = ((ListView) findViewById(R.id.good_sale_item_list));
		m_footerView = LayoutInflater.from(this).inflate(R.layout.loading, null);
		listView.addFooterView(m_footerView);
		adapter = new GoodSaleListAdapter(this);
		((GoodSaleListAdapter)adapter).setArray(array);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(GoodsSaleDeptDtlActivity.this, GoodsSaleDeptDetailDtlActivity.class);
				intent.putExtra("json", arg0.getAdapter().getItem(arg2).toString());
				startActivity(intent);
			}
		});
		start();
	}
	
	@Override
	protected void start() {
		this.map.put("deptcode", "");
		this.map.put("startdate", DateUtil.getMondayOFWeek());
		this.map.put("enddate", DateUtil.getCurrentWeekday());
		super.start();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(data == null) return;
		
		switch (requestCode) {
		case RequestListType.SEARCH_REQUEST_CODE_GoodSale:
			array.clear();
			adapter.notifyDataSetChanged();
			pageNum = 1;
			map.put("currentPage", pageNum);
			String startDate = data.getStringExtra("startdate");
			String endDate = data.getStringExtra("enddate");
			this.map.put("deptcode", data.getStringExtra("deptcode") == null ? "" : data.getStringExtra("deptcode"));
			this.map.put("startdate", startDate);
			this.map.put("enddate", endDate);
			super.start();
			break;
		default:
			break;
		}
	}
}
